import { Component, EventEmitter, Input, Output } from '@angular/core';

declare var form: any;

@Component({
  selector: 'app-userselect',
  templateUrl: './user.select.component.html',
  styleUrls: ['./user.select.component.scss']
})
export class UserSelectComponent {
  // 当前部门选择值
  key: any = null;
  // 当前用户对象
  users = [];

  // 用户表单
  fs: any = null;
  // 部门字典
  depDic: any = null;

  // 是否可用
  enable = false;
  @Input()
  get disabled() {
    return this.enable;
  }

  set disabled(value) {
    this.enable = value;
  }

  ismulti = false;
  @Input()
  get multi() {
    return this.ismulti;
  }

  set multi(value) {
    this.ismulti = value;
  }

  // 用户ID双向绑定
  private userids: string;
  @Input()
  get userIDs() {
    return this.userids;
  }

  set userIDs(value) {
    this.userids = value;
    this.init();
    this.userIDsChange.emit(value);
  }
  @Output()
  userIDsChange: EventEmitter<any> = new EventEmitter();

  // 用户名双向绑定
  private usernames: string;
  @Input()
  get userNames() {
    return this.usernames;
  }

  set userNames(value) {
    this.usernames = value;
    this.init();
    this.userNamesChange.emit(value);
  }
  @Output()
  userNamesChange: EventEmitter<any> = new EventEmitter();

  constructor() {
    this.fs = new form.FormSimpleService('userinfo', {
      keyField: 'userid',
      randomFields: ['userid'],
      orderObject: { userid: 0 },
      queryFields: 'userid,userName,uImage,organizationId,departmentId,operatingPostId,positionId,roles,employeetype'
    });
    // this.fs.loadData(true);

    // 初始化字典
    this.depDic = new form.DictionaryService('p_department');
    this.depDic.load('0', true);
  }

  // 选择
  select() {
    const us = this.users;
    this.fs.datas.forEach(e => {
      if (e.checked === true) {
        let find = false;
        us.forEach(u => {
          if (u.id === e.userid) {
            find = true;
          }
        });
        if (!find) {
          us.push({
            id: e.userid, name: e.userName,
            userOrgCode: e.organizationId, userOrgName: e.organizationName,
            userDepCode: e.departmentId, userDepName: e.departmentName,
            roles: e.roles
          });
        }
      }
    });
    this.close();
    this.giveToForm();
  }

  // 给用户字段赋值
  private giveToForm() {
    const us = [];
    if (this.users && this.users.length > 0) {
      this.users.forEach(u => {
        us.push({
          userId: u.id, userName: u.name,
          superAdmin: false, userSelected: true,
          userOrgCode: u.userOrgCode, userOrgName: u.userOrgName,
          userDepCode: u.userDepCode, userDepName: u.userDepName,
          roles: u.roles
        });
      });
    }

    if (us.length <= 0) {
      this.userNames = '';
      this.userIDs = '';
      return;
    }

    if (this.ismulti) {
      let name = ',';
      let id = ',';
      us.forEach(u => {
        name += u.userName + ',';
        id += u.userId + ',';
      });
      this.userNames = name;
      this.userIDs = id;
    } else {
      this.userNames = us[0].userName;
      this.userIDs = us[0].userId;
    }
  }

  // 初始化
  private init() {
    const us = [];
    if (this.userids && this.usernames) {
      const ids = this.userids.split(',');
      const names = this.usernames.split(',');
      for (let i = 0; i < ids.length; i++) {
        if (ids[i] !== '') {
          us.push({ id: ids[i], name: names[i] });
        }
      }
    }
    this.users = us;
  }

  // 删除
  del(item) {
    this.users.splice(this.users.indexOf(item), 1);
    this.giveToForm();
  }

  open() {
    this.fs.isEditVisible = true;
  }

  close() {
    this.fs.isEditVisible = false;
  }

  loadData() {
    if (this.key === null) {
      this.fs.queryObject['departmentId:like'] = 'nullItem';
    } else {
      this.fs.queryObject['departmentId:like'] = ',' + this.key + ',';
    }
    this.fs.loadData(true);
  }
}
